package gdconf

import (
	"server_cluster/common/logger"
)

// 角色等阶配置表

type RolePromoteConfig struct {
	Id              int32         `csv:"Id" gorm:"primary_key;autoIncrement:false"` // 角色等阶id
	PromoteGroup    int32         `csv:"PromoteGroup,omitempty"`                    // 等阶组
	Promote         int32         `csv:"Promote,omitempty"`                         // 等阶
	LevelLimit      int32         `csv:"LevelLimit,omitempty"`                      // 解锁等级上限
	NeedPlayerLevel int32         `csv:"PlayerLevel,omitempty"`                     // 所需玩家等级
	CostItemList    ItemList      `csv:"Material,omitempty"`                        // 所需材料
	Gold            int32         `csv:"GoldCoin,omitempty"`                        // 消耗金币
	FightProp1      *FightPropAdd `csv:"Attribute1,omitempty"`                      // 战斗属性1
	FightProp2      *FightPropAdd `csv:"Attribute2,omitempty"`                      // 战斗属性2
	FightProp3      *FightPropAdd `csv:"Attribute3,omitempty"`                      // 战斗属性3

	FightPropList []*FightPropAdd `gorm:"-" csv:"-"` // 战斗属性列表
}

func (r *RolePromoteConfig) TableName() string {
	return "config_role_promote"
}

func (g *GameDataConfig) saveRolePromoteConfig() {
	saveTableToDb[RolePromoteConfig](g.Db, readTable[RolePromoteConfig](g.CsvPathPrefix+"role_promote.csv"))
}

func (g *GameDataConfig) loadRolePromoteConfig() {
	g.GameDataMaps.RolePromoteConfigMap = make(map[int32]map[int32]*RolePromoteConfig)
	for _, rolePromoteConfig := range loadTableFromDb[RolePromoteConfig](g.Db) {
		_, exist := g.GameDataMaps.RolePromoteConfigMap[rolePromoteConfig.PromoteGroup]
		if !exist {
			g.GameDataMaps.RolePromoteConfigMap[rolePromoteConfig.PromoteGroup] = make(map[int32]*RolePromoteConfig)
		}
		if rolePromoteConfig.FightProp1 != nil {
			rolePromoteConfig.FightPropList = append(rolePromoteConfig.FightPropList, rolePromoteConfig.FightProp1)
		}
		if rolePromoteConfig.FightProp2 != nil {
			rolePromoteConfig.FightPropList = append(rolePromoteConfig.FightPropList, rolePromoteConfig.FightProp2)
		}
		if rolePromoteConfig.FightProp3 != nil {
			rolePromoteConfig.FightPropList = append(rolePromoteConfig.FightPropList, rolePromoteConfig.FightProp3)
		}
		g.GameDataMaps.RolePromoteConfigMap[rolePromoteConfig.PromoteGroup][rolePromoteConfig.Promote] = rolePromoteConfig
	}
	logger.Info("RolePromoteConfig Count: %v", len(g.GameDataMaps.RolePromoteConfigMap))
}

func GetRolePromoteConfigByGroupAndPromote(group int32, promote int32) *RolePromoteConfig {
	value1, exist := CONF.GameDataMaps.RolePromoteConfigMap[group]
	if !exist {
		logger.Error("[ConfigNotFound] RolePromoteConfig, group: %v, promote: %v", group, promote)
		return nil
	}
	value2, exist := value1[promote]
	if !exist {
		logger.Error("[ConfigNotFound] RolePromoteConfig, group: %v, promote: %v", group, promote)
		return nil
	}
	return value2
}

func GetRolePromoteConfigMap() map[int32]map[int32]*RolePromoteConfig {
	return CONF.GameDataMaps.RolePromoteConfigMap
}
